package com.common;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cxt
 * @date 2022/8/30
 */
@Configuration
public class RedissonConfig {
    @Value("${spring.redis.redisson.config}")
    private String redisPath;

    @Bean
    public RedissonClient redissonClient() {
        String[] split = redisPath.split("\n");
        String newPath = split.length > 2 ? split[1].split("ss:")[1].replaceAll("\"", "").replaceAll(" ", "") : "redis://127.0.0.1:6379";
        Config config = new Config();
        config.setTransportMode(TransportMode.NIO);
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(newPath);
        return Redisson.create(config);
    }
}
